<?php
class Designs extends Controller {

	function Designs()
	{
		parent::Controller();
		$this->load->helper('typography');
		$this->load->helper('text');
		$this->users->check();
	}

	function index()
	{
		$template['title'] = 'Designs';
		
		//Get all the design threads
		$data['threads'] = $this->db->query('SELECT *, COUNT(designs.designid) AS count FROM threads LEFT JOIN designs ON threads.id = designs.threadid  GROUP BY threads.id ORDER BY threads.id DESC');
		
		//Get the latest comments
		$data['comments'] = $this->db->query('SELECT * FROM comments LEFT JOIN users on users.id = comments.user LEFT JOIN threads ON threads.id = comments.thread ORDER BY comments.commentid DESC LIMIT 8');

		$template['content'] = $this->load->view('threads', $data, TRUE);
		$this->load->view('template', $template);
	}
	function thread()
	{
		//If a certain version isn't specified, show all the designs in the thread
		if ($this->uri->segment(4, 0) == '0') {
		
			// Get all designs in the current thread
			$data['query'] = $this->db->query('SELECT * FROM designs WHERE threadid = ' . $this->uri->segment(3). ' ORDER BY designid DESC');
			
			//Get the info about the current thread
			$data['thread'] = $this->db->query('SELECT * FROM threads WHERE id = ' . $this->uri->segment(3));
			foreach ($data['thread']->result() as $row) {
				$template['title'] = $row->name;
			}
			
			//Count all the designs in the thread for the links
			$this->db->where('threadid', $this->uri->segment(3));
			$this->db->from('designs');
			$data['count'] = $this->db->count_all_results();
			
			
			$template['content'] = $this->load->view('thread_all', $data, TRUE);
			$template ['tabs'] = array(
						anchor('designs', 'Back to designs'),
						);
			$this->load->view('template', $template);
		}
		
		// Single design view
		else {
			$offset = $this->uri->segment(4) -1;
			
			//Get the current thread info
			$this->db->where('id', $this->uri->segment(3));
			$data['threads'] = $this->db->get('threads');
			foreach ($data['threads']->result() as $row) {
				$template['title'] = $row->name;
			}
			
			//Get the designs in the thread
			$data['designs'] = $this->db->query('SELECT * FROM designs WHERE designs.threadid = ' . $this->uri->segment(3) . ' 
			ORDER BY designs.designid ASC LIMIT 1 OFFSET ' . $offset);
			
			//Get the comments for the design version
			foreach ($data['designs']->result() as $row) {
				$data['comments'] =  $this->db->query('SELECT *, FROM_UNIXTIME(comments.date, "%M %e") AS thedate FROM comments LEFT JOIN users ON comments.user = users.id WHERE comments.design = ' . $row->designid . ' ORDER BY commentid ASC');
			}
			
			//Count all the design versions in the thread
			$this->db->where('threadid', $this->uri->segment(3));
			$this->db->from('designs');
			$data['count'] = $this->db->count_all_results();
			
			$template ['tabs'] = array(
						anchor('designs', 'Back to designs'),
						);
			$template['content'] = $this->load->view('thread', $data, TRUE);
			$this->load->view('template', $template);
		}
	}
	function do_newthread() {
		// Insert the new thread into database
		$this->db->set('name', $_POST['title']);
		$this->db->insert('threads'); 
		redirect ('designs');
	}
	function comment() {
		// Insert the comment
		$this->db->set('design', $_POST['design']);
		$this->db->set('user', $_POST['user']);
		$this->db->set('body', auto_typography(auto_link($_POST['body'])));
		$this->db->set('date', now());
		$this->db->set('version', $_POST['thisversion']);
		$this->db->set('thread', $_POST['thisthread']);
		$this->db->insert('comments'); 
		
		//Redirect them back to the design they came from
		redirect ('designs/thread/' . $_POST['thisthread'] . '/' . $_POST['thisversion']);
	}
	function do_upload()
	{
		$config['upload_path'] = './designs/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '50000';
		$config['max_width']  = '1100';
		$config['max_height']  = '1536';

		$this->load->library('upload', $config);
		$field_name = "image";
		$thread = $_POST['thread'];
		if ( ! $this->upload->do_upload($field_name))
		{
			$template['title'] = 'Designs';
			$data['error'] = array('error' => $this->upload->display_errors());
			
			//Get all the design threads
		$data['threads'] = $this->db->query('SELECT *, COUNT(designs.designid) AS count FROM threads LEFT JOIN designs ON threads.id = designs.threadid  GROUP BY threads.id ORDER BY threads.id DESC');

		//Get the latest designs
		$data['comments'] = $this->db->query('SELECT * FROM comments LEFT JOIN users on users.id = comments.user LEFT JOIN threads ON threads.id = comments.thread ORDER BY comments.commentid DESC LIMIT 5');
		
			$data['fthreads'] = $this->db->get('threads');
			$template['content'] = $this->load->view('threads', $data, TRUE);
			$this->load->view('template', $template);
			
		}	
		else
		{
			$image = $this->upload->data();
			
			// Create the thumbnails. If you want to change the size, all you need to change is the width.
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'designs/'.$image['file_name'];
			$config['create_thumb'] = FALSE;
			$config['new_image'] = 'designs/med_'.$image['file_name'];
			$config['maintain_ratio'] = TRUE;
			$config['master_dim'] = 'width';
			$config['height'] = 525;
			$config['width'] = 550;
			$this->load->library('image_lib', $config);
			$this->image_lib->resize();
			$this->image_lib->clear();
			
			$nconfig['image_library'] = 'gd2';
			$nconfig['source_image'] = 'designs/'.$image['file_name'];
			$nconfig['create_thumb'] = FALSE;
			$nconfig['new_image'] = 'designs/small_'.$image['file_name'];
			$nconfig['maintain_ratio'] = TRUE;
			$nconfig['master_dim'] = 'width';
			$nconfig['height'] = 250;
			$nconfig['width'] = 300;
			$this->image_lib->initialize($nconfig); 
			$this->image_lib->resize();
			
			// Insert design  into database
			$this->db->set('threadid', $thread);
			$this->db->set('image', $image['file_name']);
			$this->db->set('ddate', now());
			$this->db->set('user', $this->users->getInfo($this->users->user,'id'));
			$this->db->insert('designs'); 
			
			// Redirect to the thread that the design was entered into
			redirect('designs/thread/' . $thread);
		}
	}	
}